package cn.zh.npro.Activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class Client extends BaseActivity {

    String clientId="";
    ImageView mc_back;
    TextView cliname,cuslevel,cusclass,custype,paytype,paydate,paycount;
    TextView cadress,dtadress,Taxp,Tadress,phone,usename,fadress;
    TextView bankname,banknum,cnum,crtime,reinfo,opticket,taxpoint;
    LinearLayout contactlin;
    JSONArray LinkmanList;
    String type="cftype";
    TextView stxt,ftxt;
    MyApplication mapp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);
        clientId=getIntent().getStringExtra("clId");
        type=getIntent().getStringExtra("type");
        mapp= (MyApplication) getApplication();
        initc();
         }

    @Override
    protected void onResume() {
        super.onResume();
        getCdent();
    }

    public void initc(){
        mc_back=findViewById(R.id.mc_back);
        cliname=findViewById(R.id.cliname);
        cuslevel=findViewById(R.id.cuslevel);
        cusclass=findViewById(R.id.cusclass);
        custype=findViewById(R.id.custype);
        paytype=findViewById(R.id.paytype);
        paydate=findViewById(R.id.paydate);
        paycount=findViewById(R.id.paycount);
        cadress=findViewById(R.id.cadress);
        dtadress=findViewById(R.id.dtadress);
        Taxp=findViewById(R.id.Taxp);
        Tadress=findViewById(R.id.Tadress);
        phone=findViewById(R.id.phone);
        usename=findViewById(R.id.usename);
        fadress=findViewById(R.id.fadress);
        bankname=findViewById(R.id.bankname);
        banknum=findViewById(R.id.banknum);
        cnum=findViewById(R.id.cnum);
        crtime=findViewById(R.id.crtime);
        reinfo=findViewById(R.id.reinfo);
        opticket=findViewById(R.id.opticket);
        taxpoint=findViewById(R.id.taxpoint);
        contactlin=findViewById(R.id.contactlin);
        stxt=findViewById(R.id.stxt);
        ftxt=findViewById(R.id.ftxt);
        contactlin.setOnClickListener(this);
        mc_back.setOnClickListener(this);
        if (type.equals("ortype")){
          stxt.setText("订单");
          ftxt.setVisibility(View.GONE);
        }else {
          ftxt.setVisibility(View.VISIBLE);
          stxt.setText("客户");
          ftxt.setText("编辑");
          ftxt.setOnClickListener(this);
          }
        }



    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.mc_back:
                finish();
                break;
            case R.id.contactlin:
                mapp.setClID(clientId);
                Intent intent=new Intent(Client.this,ConnectAc.class);
                intent.putExtra("id",clientId);
                startActivity(intent);
                break;
            case R.id.ftxt:
                //用户编辑
               Intent intent1=new Intent(Client.this,CliEdit.class);
               intent1.putExtra("cusId",clientId);
               startActivity(intent1);
               break;
                }
             }


    public void getCdent(){
        OkHttpUtils.get(BaseUrl.GetCustomerDetail)
                .params("customerId",clientId)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("------------->获取用户详情===="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                             JSONObject Res=js.getJSONObject("Res");
                             JSONObject Customer=Res.getJSONObject("Customer");
                             String ID=Customer.getString("ID");
                             String companyName=Customer.getString("companyName");
                             String customerType=Customer.getString("customerType");
                             String customerLevel=Customer.getString("CustomerLevel");
                             String ChargeType=Customer.getString("ChargeType");
                             String address=Customer.getString("address");
                             String joinDate=Customer.getString("joinDate");
                             String province=Customer.getString("province");
                             String city=Customer.getString("city");
                             if (companyName.equals("null")){
                                 cliname.setText("空");
                             }else {
                                 cliname.setText(companyName);
                                 }
                                if (customerLevel.equals("null")){
                                    cuslevel.setText("空");
                                }else {
                                    cuslevel.setText(customerLevel);
                                 }
                                if (customerType.equals("null")){
                                    custype.setText("空");
                                }else {
                                    custype.setText(customerType);
                                }
                                if (ChargeType.equals("null")){
                                    paytype.setText("空");
                                }else {
                                    paytype.setText(ChargeType);
                                }
                             String stim=joinDate;
                                if (joinDate.equals("null")){
                                    crtime.setText("");
                                }else {
                                    int index=stim.indexOf(".");
                                    if (-1==index){
                                    }else {
                                        stim=stim.substring(0,index);
                                    }
                                    stim=stim.replace("T"," ");
                                    crtime.setText(stim);
                                  }
                              if (ID.equals("null")){
                                  cnum.setText("");
                              }else {
                                  cnum.setText(ID);
                              }
                              if (province.equals("null")||city.equals("null")){
                              }else {
                                  cadress.setText(province+city);
                              }
                              if (address.equals("null")){
                                  dtadress.setText("");
                              }else {
                                  dtadress.setText(address);
                              }
                             LinkmanList = Res.getJSONArray("LinkmanList");

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                     }
                  });
             }


  }
